{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "impressed-accessory",
   "metadata": {},
   "source": [
    "## Seldon V2 Kubernetes Multi Version Artifact Examples\n",
    "\n",
    "We have a Triton model that has two version folders\n",
    "\n",
    "Model 1 adds 10 to input, Model 2 multiples by 10 the input. The structure of the artifact repo is shown below:\n",
    "\n",
    "```\n",
    "config.pbtxt\n",
    "1/model.py <add 10>\n",
    "2/model.py <mul 10>\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "91687b33",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"NAMESPACE\"] = \"seldon-mesh\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "sunrise-commercial",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'172.19.255.1'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MESH_IP=!kubectl get svc seldon-mesh -n ${NAMESPACE} -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "MESH_IP=MESH_IP[0]\n",
    "import os\n",
    "os.environ['MESH_IP'] = MESH_IP\n",
    "MESH_IP"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "speaking-bracelet",
   "metadata": {},
   "source": [
    "### Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "operating-console",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: math\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/samples/triton_23-03/multi-version\"\r\n",
      "  artifactVersion: 1\r\n",
      "  requirements:\r\n",
      "  - triton\r\n",
      "  - python\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/multi-version-1.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "exempt-bumper",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/math created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl apply -f ./models/multi-version-1.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "beneficial-logan",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/math condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "67900afd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"modelName\": \"math_1\",\r\n",
      "  \"modelVersion\": \"1\",\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT\",\r\n",
      "      \"datatype\": \"FP32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"4\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"fp32Contents\": [\r\n",
      "          11,\r\n",
      "          12,\r\n",
      "          13,\r\n",
      "          14\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer math --inference-mode grpc --inference-host ${MESH_IP}:80 \\\n",
    "  '{\"model_name\":\"math\",\"inputs\":[{\"name\":\"INPUT\",\"contents\":{\"fp32_contents\":[1,2,3,4]},\"datatype\":\"FP32\",\"shape\":[4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2c75517b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Model\r\n",
      "metadata:\r\n",
      "  name: math\r\n",
      "spec:\r\n",
      "  storageUri: \"gs://seldon-models/scv2/samples/triton_23-03/multi-version\"\r\n",
      "  artifactVersion: 2\r\n",
      "  requirements:\r\n",
      "  - triton\r\n",
      "  - python\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./models/multi-version-2.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "923fa31f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/math configured\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl apply -f ./models/multi-version-2.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a3b12f5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io/math condition met\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "45726caa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"modelName\": \"math_2\",\r\n",
      "  \"modelVersion\": \"1\",\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT\",\r\n",
      "      \"datatype\": \"FP32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"4\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"fp32Contents\": [\r\n",
      "          10,\r\n",
      "          20,\r\n",
      "          30,\r\n",
      "          40\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon model infer math --inference-mode grpc --inference-host ${MESH_IP}:80 \\\n",
    "  '{\"model_name\":\"math\",\"inputs\":[{\"name\":\"INPUT\",\"contents\":{\"fp32_contents\":[1,2,3,4]},\"datatype\":\"FP32\",\"shape\":[4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "precious-development",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model.mlops.seldon.io \"math\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl delete -f ./models/multi-version-1.yaml -n ${NAMESPACE}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e694edee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
